jQuery(document).ready(function($){
    jQuery('#wizard3').smartWizard({
        labelNext:'下一步',
        labelPrevious:'上一步',
        labelFinish:'完成',
        onFinish: onFinishCallback,
        onLeaveStep:leaveAStepCallback,
        onShowStep:validateAgree
    });



    function leaveAStepCallback(obj){
        var step_num= obj.attr('rel'); // get the current step number
        return validateSteps(step_num); // return false to stay on step and true to continue navigation
    }


    function validateSteps(stepnumber){
        var isStepValid = true;
        if(stepnumber == 1){
            isStepValid = validateContent() & validateDate() & validatePrice() &validateLanguage() & validateType();
            return isStepValid ;
        } else if(stepnumber == 2){
            isStepValid =validateAccount()&validateEmail()& validateCellphone();
            return isStepValid;
        }else if(stepnumber == 3){
            return isStepValid;
        }

    }

    function validateAgree(){
        var isValid = true;
        if ($('#agree').is(':checked')) {
            $(".buttonFinish").removeClass('buttonDisabled');
        } else {
            isValid=false;
            $('#agree').addClass("error");
            $(".buttonFinish").addClass('buttonDisabled');
        }
        return isValid;
    }



    function validateAccount(){
        var isValid = true;
        var co = $('#account').val();
        if(!co && co.length <= 0){
            isValid = false;
            $('#account').addClass("error");
            $('#msg_account').html('请输入您的姓名.').show();
        }else{
            $('#account').removeClass("error");
            $('#msg_account').html('').hide();
        }
        return isValid;
    }



    function validateEmail(){
        var isValid = true;
        var co = $('#email').val();
        if(!co && co.length <= 0){
            isValid = false;
            $('#email').addClass("error");
            $('#msg_email').html('请输入你的邮箱地址.').show();
        }else{
            $('#email').removeClass("error");
            $('#msg_email').html('').hide();
        }
        return isValid;
    }


    function validateCellphone(){
        var isValid = true;
        var co = $('#cellphone').val();
        if(!co && co.length <= 0){
            isValid = false;
            $('#cellphone').addClass("error");
            $('#msg_cellphone').html('请输入手机号码.').show();
        }else if(!isValidCellPhone(co)){
            isValid = false;
            $('#cellphone').addClass("error");
            $('#msg_cellphone').html('手机号码格式错误.').show();
        }else{
            $('#cellphone').removeClass("error");
            $('#msg_cellphone').html('').hide();
        }
        return isValid;
    }


    function validateType(){
        var isValid = true;
        var co = $('#type').val();
        if(!co && co.length <= 0){
            isValid = false;
            $('#type').addClass("error");
            $('#msg_type').html('请选择任务类型.').show();
        }else{
            $('#type').removeClass("error");
            $('#msg_type').html('').hide();
        }
        return isValid;
    }

    function validateLanguage(){
        var isValid = true;
        var co = $('#language').val();
        if(!co && co.length <= 0){
            isValid = false;
            $('#language').addClass("error");
            $('#msg_language').html('请选择您所需的语言.').show();
        }else{
            $('#language').removeClass("error");
            $('#msg_language').html('').hide();
        }
        return isValid;
    }


    function validatePrice(){
        var isValid = true;
        var co = $('#price').val();
        if(!co && co.length <= 0){
            isValid = false;
            $('#price').addClass("error");
            $('#msg_price').html('请输入您的价格.').show();
        }else if(!isValidNumber(co)){
            isValid = false;
            $('#price').addClass("error");
            $('#msg_price').html('请输入数字.').show();
        }else{
            $('#price').removeClass("error");
            $('#msg_price').html('').hide();
        }
        return isValid;
    }
    function validateDate(){
        var isValid = true;
        // Validate Username
        var st = $('#startdatepic').val();
        var et = $('#enddatepic').val();
        if(!st && st.length <= 0){
            isValid = false;
            $('#startdatepic').addClass("error");
            $('#msg_data').html('请输入开始日期.').show();
        }else if(!et && et.length <= 0){
            isValid = false;
            $('#enddatepic').addClass("error");
            $('#msg_data').html('请输入结束日期.').show();
        }else if(new Date(st)>new Date(et)){
            isValid = false;
            $('#enddatepic').addClass("error");
            $('#msg_data').html('结束日期需大于开始日期.').show();
        }else {
            $('#startdatepic').removeClass("error");
            $('#enddatepic').removeClass("error");
            $('#msg_data').html('').hide();
        }
        return isValid;
    }

    function validateContent(){
        var isValid = true;
        // Validate Username
        var co = $('#content').val();
        if(!co && co.length <= 0){
            isValid = false;
            $('#content').addClass("error");
            $('#msg_content').html('请输入您的任务内容.').show();
        }else if(!co && co.length >= 400){
            isValid = false;
            $('#content').addClass("error");
            $('#msg_content').html('您输入内容过长.').show();
        }
         else{
                $('#content').removeClass("error");
                $('#msg_content').html('').hide();
            }

        return isValid;
    }

    $("#agree").bind("change",function(event){
        validateAgree();
    });



    $("#account").bind("change",function(event){
        validateAccount();
    });


    $("#email").bind("change",function(event){
        validateEmail();
    });


    $("#cellphone").bind("change",function(event){
        validateCellphone();
    });


    $("#type").bind("change",function(event){
        validateType();
    });

    $("#language").bind("change",function(event){
        validateLanguage();
    });

    $("#price").bind("change",function(event){
        validatePrice();
    });
    $("#enddatepic").bind("change",function(event){
        validateDate();
    });
    $("#startdatepic").bind("change",function(event){
        validateDate();
    });
    $("#content").bind("input propertychange",function(event){
        validateContent();
    });

    function onFinishCallback(){
        $("#create_task").submit();
    }


    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    }

    function isValidNumber(prices) {
        var pattern = new RegExp(/^(\d{0,9})?$/);
        return pattern.test(prices);
    }


    function isValidCellPhone(cellphone) {
        var pattern = new RegExp(/^(13[0-9]|15[0|3|6|7|8|9]|18[6|8|9])\d{8}$/);
        return pattern.test(cellphone);
    }






    jQuery(".inline").colorbox({inline:true, width: '60%', height: '500px'});

    /*** pagination ***/
    jQuery('.pagination a').click(function(){
        var p = jQuery(this).parent();
        if(!p.hasClass('previous') && !p.hasClass('first') && !p.hasClass('next') && !p.hasClass('last')) {
            jQuery('.pagination a').each(function(){
                jQuery(this).removeClass('current');
            });
            jQuery(this).addClass('current');

            //disable next and last button when active page is the last page
            if(jQuery(this).parent().next().hasClass('next')) {
                jQuery('.pagination .next a, .pagination .last a').addClass('disable');
            } else {
                jQuery('.pagination .next a, .pagination .last a').removeClass('disable');
            }

            //disable first and previous button when active page is the first page
            if(jQuery(this).parent().prev().hasClass('previous')) {
                jQuery('.pagination .previous a, .pagination .first a').addClass('disable');
            } else {
                jQuery('.pagination .previous a, .pagination .first a').removeClass('disable');
            }

        }
        return false;
    });

    //clicking next button
    jQuery('.pagination li.next a').click(function(){
        if(!jQuery(this).hasClass('disable')) {
            if(!jQuery(this).parent().prev().find('a').hasClass('current')) {
                jQuery('.pagination a.current').removeClass('current').parent().next().find('a').addClass('current');
            }
        }
        if(jQuery('.pagination a.current').parent().next().hasClass('next')) {
            jQuery('.pagination .next a, .pagination .last a').addClass('disable');
        }
        if(!jQuery('.pagination a.current').parent().prev().hasClass('previous')) {
            jQuery('.pagination .previous a, .pagination .first a').removeClass('disable');
        }

    });


    //clicking previous button
    jQuery('.pagination li.previous a').click(function(){
        if(!jQuery(this).hasClass('disable')) {
            if(!jQuery(this).parent().next().find('a').hasClass('current')) {
                jQuery('.pagination a.current').removeClass('current').parent().prev().find('a').addClass('current');
            }
        }
        if(jQuery('.pagination a.current').parent().prev().hasClass('previous')) {
            jQuery('.pagination .first a, .pagination .previous a').addClass('disable');
        }
        if(!jQuery('.pagination a.current').parent().next().hasClass('next')) {
            jQuery('.pagination .next a, .pagination .last a').removeClass('disable');
        }

    });

    //clicking last button
    jQuery('.pagination .last a').click(function(){
        jQuery(this).addClass('disable');
        jQuery('.pagination .next a').addClass('disable');
        jQuery('.pagination .current').removeClass('current');
        jQuery('.pagination .next a').parent().prev().find('a').addClass('current');
        jQuery('.pagination .first a, .pagination .previous a').removeClass('disable');
    });

    //clicking last button
    jQuery('.pagination .first a').click(function(){
        jQuery(this).addClass('disable');
        jQuery('.pagination .previous a').addClass('disable');
        jQuery('.pagination .current').removeClass('current');
        jQuery('.pagination .previous a').parent().next().find('a').addClass('current');
        jQuery('.pagination .last a, .pagination .next a').removeClass('disable');
    });


    //show tabbed widget
    jQuery('#tabs').tabs();


    jQuery("#startdatepic" ).datepicker({
        dayNamesMin: [ "周日", "周一", "周二", "周三", "周四", "周五", "周六" ],
        monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        gotoCurrent: true,
        dateFormat: "yy-mm-dd",
        showCurrentAtPos: 0,
        setDate: new Date(),
        defaultDate:new Date(),
        showButtonPanel:true,
        minDate: '+2',
        currentText:'今天',
        closeText:'取消'
    });
    jQuery( "#enddatepic" ).datepicker({
        dayNamesMin: [ "周日", "周一", "周二", "周三", "周四", "周五", "周六" ],
        monthNames: [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        gotoCurrent: true,
        dateFormat: "yy-mm-dd",
        showCurrentAtPos: 0,
        setDate:  new Date(),
        showButtonPanel:true,
        minDate: '+2',
        currentText:'今天',
        closeText:'取消'
    });



    jQuery.datepicker._gotoToday =function(id) {
        var target = jQuery(id);
        var inst = this._getInst(target[0]);
        if (this._get(inst, 'gotoCurrent') && inst.currentDay) {
            inst.selectedDay = inst.currentDay;
            inst.drawMonth = inst.selectedMonth = inst.currentMonth;
            inst.drawYear = inst.selectedYear = inst.currentYear;
        }else {
            var date = new Date();
            inst.selectedDay = date.getDate();
            inst.drawMonth = inst.selectedMonth = date.getMonth();
            inst.drawYear = inst.selectedYear = date.getFullYear();
            // the below two lines are new
            this._setDateDatepicker(target, date);
            this._selectDate(id, this._getDateDatepicker(target));
        }
        this._notifyChange(inst);
        this._adjustDate(target);
    }

});